package com.zhc.dejingapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhc.dejingapi.common.result.Result;
import com.zhc.dejingapi.entity.QingBoilingreportEntity;
import com.zhc.dejingapi.entity.QingProcessplanEntity;
import com.zhc.dejingapi.entity.query.CodeQuery;
import com.zhc.dejingapi.entity.vo.QingInjectAndSendWorkVo;
import com.zhc.dejingapi.mapper.QingBoilingreportMapper;
import com.zhc.dejingapi.mapper.QingProcessplanMapper;
import com.zhc.dejingapi.service.QingBoilingreportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 水处理派工单 服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-10-16
 */
@Service
public class QingBoilingreportServiceImpl extends ServiceImpl<QingBoilingreportMapper, QingBoilingreportEntity> implements QingBoilingreportService {

    @Resource
    private QingBoilingreportMapper qingBoilingreportMapper;

    @Resource
    private QingProcessplanMapper qingProcessplanMapper;
    @Override
    public Result reportBoilJob(QingInjectAndSendWorkVo qingInjectAndSendWorkVo) {
        QingBoilingreportEntity qingBoilingreportEntity = new QingBoilingreportEntity();
        BeanUtil.copyProperties(qingInjectAndSendWorkVo,qingBoilingreportEntity);
        qingBoilingreportEntity.setMainId(null);
        int insert = qingBoilingreportMapper.insert(qingBoilingreportEntity);
        if(insert > 0){
            QueryWrapper<QingProcessplanEntity> qW1=new QueryWrapper<>();

            qW1.eq("process_plan_code",qingBoilingreportEntity.getProcessPlanCode());
            QingProcessplanEntity qingProcessplan1 = qingProcessplanMapper.selectOne(qW1);
            int totalReportNum1 =0;
            if(qingProcessplan1.getTotalReportNum()!=null){
                totalReportNum1=qingProcessplan1.getTotalReportNum();
            }
            qingProcessplan1.setTotalReportNum(totalReportNum1+qingBoilingreportEntity.getEachCartonQty());
            qingProcessplanMapper.update(qingProcessplan1,qW1);
            return Result.ok(qingBoilingreportEntity);
        }else {
            return Result.fail(null);
        }
    }

    @Override
    public PageInfo<QingBoilingreportEntity> getReportFinishBoil(CodeQuery codeQuery) {
        int current=codeQuery.getPageCurrent();
        int size=codeQuery.getPageSize();

        //获取第1页，10条内容，默认查询总数count
        PageHelper.startPage(current, size);
        QueryWrapper<QingBoilingreportEntity> queryWrapper=new QueryWrapper<>();
        if(codeQuery.getReportJobTime()!=null){
            queryWrapper.likeRight("boiling_start_time",codeQuery.getReportJobTime());
        }

        //只有紧跟着PageHelper.startPage()的sql起了作用
        List<QingBoilingreportEntity> codeList = qingBoilingreportMapper.selectList(queryWrapper);
        //用PageInfo对结果进行包装
        PageInfo<QingBoilingreportEntity> page = new PageInfo<>(codeList);

        return page;
    }
}
